package gxmzu.gxmzuproject.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import gxmzu.gxmzuproject.config.redisConfig.RedisService;
import gxmzu.gxmzuproject.config.redisConfig.RedisUtil;
import gxmzu.gxmzuproject.entity.MiniUserInfo;
import gxmzu.gxmzuproject.mapper.MiniUserInfoMapper;
import gxmzu.gxmzuproject.param.MiniCode2SessionQueryParam;
import gxmzu.gxmzuproject.service.MiniUserInfoService;
import gxmzu.gxmzuproject.param.MiniUserInfoQueryParam;
import gxmzu.gxmzuproject.utils.OkHttpUtil;
import gxmzu.gxmzuproject.vo.MiniLoginInfoQueryVo;
import gxmzu.gxmzuproject.vo.MiniUserInfoQueryVo;
import gxmzu.gxmzuproject.config.common.BaseServiceImpl;
import gxmzu.gxmzuproject.config.common.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Code534
 * @since 2022-08-03
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class MiniUserInfoServiceImpl extends BaseServiceImpl<MiniUserInfoMapper, MiniUserInfo> implements MiniUserInfoService {

    @Resource
    private MiniUserInfoMapper miniUserInfoMapper;

    @Resource
    private OkHttpUtil okHttpUtil;

    @Resource
    RedisService redisService;

    @Override
    public MiniUserInfoQueryVo getMiniUserInfoById(Serializable id) {
        return miniUserInfoMapper.getMiniUserInfoById(id);
    }

    @Override
    public Paging<MiniUserInfoQueryVo> getMiniUserInfoPageList(MiniUserInfoQueryParam param) {
        IPage<MiniUserInfoQueryVo> iPage = miniUserInfoMapper.getMiniUserInfoPageList(getPage(param),param);
        return new Paging<>(iPage);
    }

    @Override
    public MiniLoginInfoQueryVo UserLogin(MiniCode2SessionQueryParam miniCode2SessionQueryParam) {

        //请求微信服务器返回数据

        //登录小程序请求要求的固定参数
        String appSecret = "204e729ea90d3327f3c12ae08a3ca6b7";
        String appId = "wx154e6a46cc2ee18c";
        String grant_type = "authorization_code";
        String url = "https://api.weixin.qq.com/sns/jscode2session";

        //注入参数
        miniCode2SessionQueryParam.setSecret(appSecret);
        miniCode2SessionQueryParam.setGrant_type(grant_type);
        miniCode2SessionQueryParam.setAppid(appId);

        //请求微信服务器拿到用户的openid、session_key
        BeanMap beanMap = BeanMap.create(miniCode2SessionQueryParam);
        String code2SessionJson = okHttpUtil.get(url, beanMap);

        //请求失败
        if (code2SessionJson == null) {
            return null;
        }


        //json解析
        MiniLoginInfoQueryVo miniLoginInfoQueryVo = new Gson().fromJson(code2SessionJson, MiniLoginInfoQueryVo.class);

        //请求成功，没有错误码
        if (miniLoginInfoQueryVo.getErrcode() == null) {

            //请求成功,openid插入数据库插入redis，mysql
            if (miniLoginInfoQueryVo.getOpenid() != null) {


                //redis没有缓存该数据则存入数据库，并存入redis
                if (redisService.get(miniLoginInfoQueryVo.getOpenid()) == null) {

                    //用户插入数据库
                    MiniUserInfo info = new MiniUserInfo();
                    info.setOpenId(miniLoginInfoQueryVo.getOpenid());
                    miniUserInfoMapper.insert(info);


                    //用户插入redis，value默认为1
                    redisService.set(miniLoginInfoQueryVo.getOpenid(), "1");
                    //redis用户数量统计
                    redisService.incrBy("allUser", 1);
                }




            }

        }

        return miniLoginInfoQueryVo;

    }

}
